package my.diplom.dao.impl;

import my.diplom.dao.IRoleDAO;
import my.diplom.entity.Role;
import org.springframework.orm.hibernate3.HibernateTemplate;
import org.apache.log4j.Logger;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: gb
 * Date: 14.07.2009
 * Time: 16:38:28
 * To change this template use File | Settings | File Templates.
 */
public class RoleDAOImpl implements IRoleDAO {

     private static final Logger log = Logger.getLogger(RoleDAOImpl.class);

    private HibernateTemplate template;

    public void setTemplate(HibernateTemplate template) {
        this.template = template;
    }

    public void create(Role role) {
        log.debug("create role = " + role.getRoleName());
        template.save(role);
    }

    public void update(Role role) {
        log.debug("update role = " + role.getRoleName());
        template.saveOrUpdate(role);
    }

    public List<Role> getAll() {
        log.debug("get all roles");
        //noinspection JpaQlInspection
        return template.find("FROM Role order by roleName");
    }

    public Role getById(Long roleId) {
        log.debug("get role by id = " + roleId);
        return (Role)template.load(Role.class, roleId);
    }

    public Role getByName(String roleName) {
        log.debug("get role by role name = " + roleName);
        //noinspection JpaQlInspection
        return (Role)(template.find("from Role r where r.roleName = ?", roleName).get(0));
    }


    public void delete(Role role) {
        log.debug("delet role = " + role.getRoleName());
        template.delete(role);
    }
}
